package com.fan.shop.services;

import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

import org.springframework.transaction.annotation.Transactional;

import com.fan.shop.dao.BrandDao;
import com.fan.shop.domain.Brand;
import com.fan.shop.domain.Item;
import com.fan.shop.global.ShopGlobalVariables;
import com.fan.shop.model.userclass.BrandUserClass;

@Named("brandService")
@Transactional(readOnly = true)
public class BrandServiceImpl implements BrandService {

	@Inject
	@Named("brandDao")
	private BrandDao brandDao;

	public BrandDao getBrandDao() {
		return brandDao;
	}

	public void setBrandDao(BrandDao brandDao) {
		this.brandDao = brandDao;
	}

	@SuppressWarnings("rawtypes")
	public List getList() {
		return brandDao.getAllBrands();
	}

	public int getColumnsSize() {
		return brandDao.getColumnNames().size();
	}

	public Brand getBrand(String name) {
		return brandDao.getBrand(name);
	}

	public List<Brand> getAllBrands() {
		return brandDao.getAllBrands();
	}

	public int getSize() {
		return brandDao.getSize();
	}

	@Transactional(readOnly = false)
	public void addBrand(Brand brand) {
		brandDao.addBrand(brand);

	}

	@Transactional(readOnly = false)
	public void updateStock(Brand brand) {
		brandDao.updateStock(brand);
	}

	@Transactional(readOnly = false)
	public void deleteBrand(Brand brand) {
		brandDao.deleteBrand(brand);
	}

	public boolean hasBrand(String name) {
		return brandDao.hasBrand(name);
	}

	public List<String> getColumnNames() {

		return brandDao.getColumnNames();
	}

	public Object copy(Object obj) {

		Brand brand = (Brand) obj;
		BrandUserClass brandUserClass = new BrandUserClass();
		brandUserClass.setName(brand.getName());
		brandUserClass.setDescription(brand.getDescription());
		brandUserClass.setIcon("<img class='thumbnail' src='/"
				+ ShopGlobalVariables.MODULE_NAME + brand.getIcon()
				+ "' height='42' width='42' />");
		brandUserClass.setEnabled(brand.getEnabled());
		return brandUserClass;
	}

	public List<Item> getItemsWithBrand(String brand) {
		return brandDao.getItemsWithBrand(brand);
	}

	public boolean getHasBrandItemsFlag(String brand) {
		if (getItemsWithBrand(brand) == null
				|| getItemsWithBrand(brand).size() == 0) {
			return false;
		} else {
			return true;
		}
	}

}
